package com.situ.company.web220111.department.dao.impl;

import com.situ.company.web220111.department.dao.IDepartmentDao;
import com.situ.company.web220111.department.model.DepartmentModel;
import com.situ.company.web220111.util.FmtEmpty;
import com.situ.company.web220111.util.JDBCUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// 数据层 = sql + 参数 + 映射
public class Department2DaoImpl implements IDepartmentDao {
    private static String table = "department";
    private static String cols = "code,name,tel";

    @Override
    public String insert(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("insert into ").append(table).append('(').append(cols).append(")values(?,?,?)");
        return JDBCUtil.update(sql.toString(), model.getCode(), model.getName(), model.getTel());
    }

    @Override
    public String delete(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("delete from ");
        sql.append(table).append(" where code = ?");
        return JDBCUtil.update(sql.toString(), model.getCode());
    }

    @Override
    public String updateAll(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("update ");
        sql.append(table).append(" set name = ?,tel  = ? where code = ?");
        return JDBCUtil.update(sql.toString(), model.getName(), model.getTel(), model.getCode());
    }

    @Override
    public String updateActive(DepartmentModel model) {
        String sql = String.format("update %s set name = ?,tel = ? where code = ?", table);
        return JDBCUtil.update(sql, model.getName(), model.getTel(), model.getCode());
    }

    private List<Object> appendSet(DepartmentModel model, StringBuffer sql) {
        List<Object> vals = new ArrayList<>();
        sql.append(table).append("set id = id ");
        String name = model.getName();
        if (name != null) { // name == null 没有对象|| name.trim().isEmpty() 有对象但是没有数据
            sql.append(", name = ? ");
            vals.add(name);
        }
        String tel = model.getTel();
        if (tel != null) { // name == null 没有对象|| name.trim().isEmpty() 有对象但是没有数据
            sql.append(",tel = ? ");
            vals.add(tel);
        }
        sql.append("where code = ?");
        vals.add(model.getCode());
        return vals;
    }

    @Override
    public List<DepartmentModel> selectList(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("select id,").append(cols).append(",(select count(1) from employee where code_dept = department.code) count ");
        sql.append(" from ").append(table);
        List vals = appendWhere(model, sql);
        return JDBCUtil.querylist(sql.toString(), vals, DepartmentModel.class);
    }


    private List<Object> appendWhere(DepartmentModel model, StringBuffer sql) {
        sql.append(" where 1=1 ");
        List<Object> vals = new ArrayList<>();
        String code = model.getCode();
        if (!FmtEmpty.isEmpty(code)) {
            sql.append(" and code like ? ");
            vals.add(code);
        }
        String name = model.getName();
        if (!FmtEmpty.isEmpty(name)) {
            sql.append(" and name like ? ");
            vals.add(name);
        }
        String tel = model.getTel();
        if (!FmtEmpty.isEmpty(tel)) {
            sql.append(" and tel like ? ");
            vals.add(tel);
        }
        if (model.isPageOn()){
            sql.append(" limit ?,? ");
            // -查询第1页的10条记录  -- 0,10
            // -查询第2页的10条记录  -- 10,10
            // -查询第3页的10条记录  -- 20,10
            // pageIndex:第几页 pageLimit:几条记录 -- (pageIndex-1)*pageLimit
            vals.add(model.getRowStart());
            vals.add(model.getPageLimit());
        }
        return vals;
    }

    @Override
    public DepartmentModel selectModel(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("select id,").append(cols);
        sql.append(" from ").append(table).append(" where code = ? ");
        List<Object> values = Arrays.asList(model.getCode());
        return JDBCUtil.queryModel(sql.toString(), values, DepartmentModel.class);
    }

    @Override
    public Integer selectCount(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("select count(1) from ").append(table);
        List<Object> vals = appendWhere(model,sql);
        return JDBCUtil.queryInt(sql.toString(),vals);
    }
}
